package com.guo.hui.service;

import com.alibaba.fastjson.JSONObject;
import com.guo.hui.dao.MyAccountMapper;
import com.guo.hui.entity.InoutType;
import com.guo.hui.entity.MyAccount;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName MyAccountServiceImpl
 * @Description TODO
 * @Author zhengguohui
 * @Date 2018/8/9 10:45
 */

@Service("myAccountService")
public class MyAccountServiceImpl implements MyAccountService {

    @Autowired
    private MyAccountMapper myAccountMapper;


    @Override
    public void saveAccount(JSONObject object) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        MyAccount account = object.toJavaObject(MyAccount.class);
        account.setRecordTime(format.format(new Date()));
        if (InoutType.IN.getCode().equals(account.getInoutCode())) {
            account.setInoutName(InoutType.IN.getName());
        }
        if (InoutType.OUT.getCode().equals(account.getInoutCode())) {
            account.setInoutName(InoutType.OUT.getName());
        }
        myAccountMapper.insert(account);
    }

    @Override
    public List<MyAccount> getAccountList(JSONObject object) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Map<String, Object> map = new HashMap<>();
        map.put("inoutCode", object.getString("inoutCode"));
        map.put("startmoney", object.getBigDecimal("startmoney"));
        map.put("endmoney", object.getBigDecimal("endmoney"));
        String starttime = "";
        Object data = object.get("starttime");
        if (data != null && !"".equals(data.toString())) {
            starttime = format.format(object.getDate("starttime"));
        }
        String endtime = null;
        Object data1 = object.get("endtime");
        if (data1 != null && !"".equals(data1.toString())) {
            endtime = format.format(object.getDate("endtime"));
        }
        map.put("starttime", starttime);
        map.put("endtime", endtime);
        List<MyAccount> list = myAccountMapper.findByMap(map);
        return list;
    }


    @Override
    public void updateAccount(JSONObject object) {
        myAccountMapper.updateByKey(null);
    }

}
